<?php
class Application_Model_Conta extends Zend_Db_Table_Abstract
{
	// Table Name
	protected $_name = 'contas';
	// Primary Key
	protected $_primary = 'id_contas';
	
	// Insert
	public function insert(array $data)
	{
		parent::insert($data);
	}
	
	// Update
	public function update(array $data, $where)
	{
		parent::update($data, $where);
	}
	
	// Delete
	public function delete($where)
	{
		parent::delete($where);
	}
	
 	public function selectAll()
    {
      $select = $this->select()->setIntegrityCheck(false);
      
      $select->from(array('c' => $this->_name), array('c.valor','c.id_contas'));
      
      $select->join(array('d' => 'dia'), 'd.id_dia = c.id_dia',array(new Zend_Db_Expr("to_char(d.dia, 'dd-mm-YYYY') as dia")));
      
      $select->join(array('tp' => 'tipo_contas'),'c.id_tipo_contas = tp.id_tipo_contas', array('tp.descricao'));
      
      $select->order('d.id_dia ASC');
      
      return $this->fetchAll($select);
    }
    public function selectBetween($id_1,$id_2)
    {
    	$select = $this->select()->setIntegrityCheck(false);
    	
    	$select->from(array('c' => $this->_name), array('c.valor','c.id_contas'));
    	
    	$select->join(array('d' => 'dia'), 'd.id_dia = c.id_dia',array(new Zend_Db_Expr("to_char(d.dia, 'dd-mm-YYYY') as dia")));
    	
    	$select->join(array('tp' => 'tipo_contas'),'c.id_tipo_contas = tp.id_tipo_contas', array('tp.descricao'));
    	
     	$select->where('d.id_dia >= ?',$id_1);
		    	
     	$select->where('c.id_dia < ?',$id_2);
     	
    	$select->order('c.id_dia ASC');
    
    	return $this->fetchAll($select);
    }
    public function selectRow($id)
    {
    	$select = $this->select()->setIntegrityCheck(false);
    
    	$select->from(array('c' => $this->_name), array('c.valor','c.id_contas','c.id_dia'));
    
    	$select->join(array('d' => 'dia'), 'd.id_dia = c.id_dia',array('d.dia'));
    
    	$select->join(array('tp' => 'tipo_contas'),'c.id_tipo_contas = tp.id_tipo_contas', array('tp.descricao','tp.id_tipo_contas'));
    
    	$select->where('c.id_contas = ?',$id);
    	
    	return $this->fetchRow($select);
    }
}